package com.retlife.tvshow.ui

import android.os.Bundle
import android.view.KeyEvent
import android.view.KeyEvent.ACTION_DOWN
import com.retlife.tvshow.R
import com.retlife.tvshow.global.EventBusMsg
import com.retlife.tvshow.global.EventBusOrder
import com.retlife.tvshow.global.RunningContext
import com.retlife.tvshow.view.GameView
import org.greenrobot.eventbus.EventBus
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode
import java.util.concurrent.ScheduledExecutorService
import java.util.concurrent.ScheduledFuture
import java.util.concurrent.ScheduledThreadPoolExecutor
import java.util.concurrent.ThreadFactory

class GameActivity : BaseActivity() {
    private lateinit var gameView: GameView
    private val service: ScheduledExecutorService = ScheduledThreadPoolExecutor(2, ThreadFactory { Thread(it, "game") })
    private var scheduleAtFixedRate: ScheduledFuture<*>? = null
    private lateinit var pauseDialog: PauseDialog
    val intArray = mutableListOf<Int>()
    val booleanArray = mutableListOf<Boolean>()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)


        pauseDialog = PauseDialog(this)
        gameView = findViewById(R.id.game_view)
        gameView.setPlayerCount(RunningContext.maxConnect)
        gameView.start()

        EventBus.getDefault().register(this)
    }

    override fun getLayoutId(): Int {
        return R.layout.activity_game
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    fun onEvent(event: EventBusMsg) {
        when (event.tag) {
            EventBusOrder.ACTION_QUIT -> {
                if (pauseDialog.isVisible()) {
                    pauseDialog.dismiss()
                    finish()
                }
            }

            EventBusOrder.ACTION_RESTART -> {
                if (pauseDialog.isVisible()) {
                    pauseDialog.dismiss()
                    gameView.reStart()
                }
            }

            EventBusOrder.ACTION_RESUME -> {
                if (pauseDialog.isVisible()) {
                    pauseDialog.dismiss()
                    gameView.resume()
                }
            }

            EventBusOrder.TIMER -> {
                runOnUiThread {
                    /*for (i in 0 until RunningContext.bikeDataList.size) {
                        if (i >= intArray.size) {
                            intArray.add(RunningContext.bikeDataList[i].speed)
                            booleanArray.add(RunningContext.bikeDataList[i].isActive)
                        } else {
                            intArray[i] = RunningContext.bikeDataList[i].speed
                            booleanArray[i] = RunningContext.bikeDataList[i].isActive
                        }
                    }*/
                    gameView.changeProcess(RunningContext.bikeDataList)
//                    gameView.changeBikeMode(booleanArray)
                }
            }
        }
    }

    override fun onKeyDown(keyCode: Int, event: KeyEvent?): Boolean {
        if (event?.action == ACTION_DOWN) {

            if (keyCode == KeyEvent.KEYCODE_BACK && !pauseDialog.isVisible()) {
                pauseDialog.show()
                gameView.pause()
                return true
            }
           /* else if (keyCode == KeyEvent.KEYCODE_DPAD_DOWN_LEFT) {
                if (pauseDialog.isVisible()) {
                    pauseDialog.dismiss()
                    finish()
                    return true
                }
            } else if (keyCode == KeyEvent.KEYCODE_DPAD_DOWN_RIGHT) {
                if (pauseDialog.isVisible()) {
                    pauseDialog.dismiss()
                    gameView.reStart()
                    return true
                }
            } else if (keyCode == KeyEvent.KEYCODE_ENTER) {
                if (pauseDialog.isVisible()) {
                    pauseDialog.dismiss()
                    gameView.resume()
                    return true
                }
            }*/
        }

        return super.onKeyDown(keyCode, event)
    }

    override fun onStop() {
        super.onStop()
        gameView.stop()
        EventBus.getDefault().unregister(this)
        if (scheduleAtFixedRate != null)
            scheduleAtFixedRate?.cancel(true)
    }
}